import datetime as dt

from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse

from settings import TEMPLATES as templates

from database import mongo_cli

router = APIRouter(prefix='/my_lover')


@router.get('/')
async def my_lover(request: Request):
    col = mongo_cli['OurStory']
    our_storys = []
    for story in col.find().sort('number'):
        story['date'] = dt.datetime.fromtimestamp(story['time']).strftime('%Y年%m月%d日')
        our_storys.append(story)
    context = {
        'request': request,
        'our_story': our_storys
    }
    return templates.TemplateResponse('index.html', context)
